package com.example.newland.co2;

import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SettingActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText setting_ed_ip;
    private EditText setting_ed_port;
    private EditText setting_ed_projice;
    private EditText setting_ed_wangguan;
    private EditText setting_ed_lora;
    private EditText setting_ed_fans;
    private EditText setting_ed_ch4;
    private EditText setting_ed_shangxian;
    private Button setting_but_quxiao;
    private Button setting_but_baocun;
    private TextView setting_tv_fanhui;
    private SharedPreferences config;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        //查找控件
        setting_ed_ip = findViewById(R.id.setting_ed_ip);
        setting_ed_port = findViewById(R.id.setting_ed_port);
        setting_ed_projice = findViewById(R.id.setting_ed_projice);
        setting_ed_wangguan = findViewById(R.id.setting_ed_wangguan);
        setting_ed_lora = findViewById(R.id.setting_ed_lora);
        setting_ed_fans = findViewById(R.id.setting_ed_fans);
        setting_ed_ch4 = findViewById(R.id.setting_ed_ch4);
        setting_ed_shangxian = findViewById(R.id.setting_ed_shangxian);
        setting_but_quxiao = findViewById(R.id.setting_but_quxiao);
        setting_but_baocun = findViewById(R.id.setting_but_baocun);
        setting_tv_fanhui = findViewById(R.id.setting_tv_fanhui);

        setting_but_quxiao.setOnClickListener(this);
        setting_but_baocun.setOnClickListener(this);
        setting_tv_fanhui.setOnClickListener(this);

        //创建一个share对象
        config = getSharedPreferences("config", MODE_PRIVATE);

        String setting_ed_ip_tre = config.getString(XindaluConfig.setting_ed_ip, "");
        String setting_ed_port_tre = config.getString(XindaluConfig.setting_ed_port, "");
        String setting_ed_projice_tre = config.getString(XindaluConfig.setting_ed_projice, "");
        String setting_ed_wangguan_tre = config.getString(XindaluConfig.setting_ed_wangguan, "");
        String setting_ed_lora_tre = config.getString(XindaluConfig.setting_ed_lora, "");
        String setting_ed_fans_tre = config.getString(XindaluConfig.setting_ed_fans, "");
        String setting_ed_ch4_tre = config.getString(XindaluConfig.setting_ed_ch4, "");
        String setting_ed_shangxian_tre = config.getString(XindaluConfig.setting_ed_shangxian, "");


        //以文本的方式显示
        setting_ed_ip.setText(setting_ed_ip_tre);
        setting_ed_port.setText(setting_ed_port_tre);
        setting_ed_projice.setText(setting_ed_projice_tre);
        setting_ed_wangguan.setText(setting_ed_wangguan_tre);
        setting_ed_lora.setText(setting_ed_lora_tre);
        setting_ed_fans.setText(setting_ed_fans_tre);
        setting_ed_ch4.setText(setting_ed_ch4_tre);
        setting_ed_shangxian.setText(setting_ed_shangxian_tre);
    }

    @Override
    public void onClick(View view) {
        switch(view.getId()){
            case R.id.setting_but_baocun:
                String vie_ip = setting_ed_ip.getText().toString();
                String vie_port = setting_ed_port.getText().toString();
                String vie_projice = setting_ed_projice.getText().toString();
                String vie_wangguan = setting_ed_wangguan.getText().toString();
                String vie_lora = setting_ed_lora.getText().toString();
                String vie_fans = setting_ed_fans.getText().toString();
                String vie_ch4 = setting_ed_ch4.getText().toString();
                String vie_shangxian = setting_ed_shangxian.getText().toString();

                SharedPreferences.Editor edit = config.edit();

                edit.putString(XindaluConfig.setting_ed_ip,vie_ip);
                edit.putString(XindaluConfig.setting_ed_port,vie_port);
                edit.putString(XindaluConfig.setting_ed_projice,vie_projice);
                edit.putString(XindaluConfig.setting_ed_wangguan,vie_wangguan);
                edit.putString(XindaluConfig.setting_ed_lora,vie_lora);
                edit.putString(XindaluConfig.setting_ed_fans,vie_fans);
                edit.putString(XindaluConfig.setting_ed_ch4,vie_ch4);
                edit.putString(XindaluConfig.setting_ed_shangxian,vie_shangxian);

                edit.commit();

                break;
            case R.id.setting_but_quxiao:
                finish();
                break;
            case R.id.setting_tv_fanhui:
                finish();
                break;
        }
    }
}
